package bdz.projects.atwww.electrolux.views.digital
{
	import bdz.projects.atwww.electrolux.events.ELEvent;
	import bdz.projects.atwww.electrolux.events.ELUIEvent;
	import bdz.projects.atwww.electrolux.model.core.ELOption;
	import bdz.projects.atwww.electrolux.ui.ELOptionsControl;
	import bdz.projects.atwww.electrolux.views.digital.support.ELDigitalOptionIndicator;
	import bdz.projects.atwww.electrolux.views.digital.support.ELDigitalOptionItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	
	import gs.TweenMax;
	import gs.easing.Strong;

	public class ELDigitalOptions extends ELOptionsControl {
		private var _optionclips:Array;
		private var _optClicker:SimpleButton;
		private var _prevClicker:SimpleButton;
		private var _favClicker:SimpleButton;
		private var _nextClicker:SimpleButton;
		private var _delayClicker:SimpleButton;
		private var _tmClicker:SimpleButton;
		private var _inprogressClip:MovieClip;
		private var _activeOption:ELDigitalOptionItem;
		private var _remainMarker:MovieClip;
		
		private var _currentOpt:Number = 0;
		private var _maxOpt:Number = 3;
		
		private var _optWidth:Number = 136;
		
		private var _optionIndicator:ELDigitalOptionIndicator;
		private var _delayOption:ELOption;
		private var _delayActive:Boolean = false;
		private var _tmActive:Boolean = false;
		private var _tmOption:ELOption;
		
		private var _arrowLeft:MovieClip;
		private var _arrowRight:MovieClip;
		private var _favSaved:MovieClip;
		
		public function ELDigitalOptions() {
			super();
			
			createDelayOption();
		}
		private function assignReferences():void{
			
			
			_optionIndicator = this["optionIndicator"];
			_favSaved = this["favsaved"];
			//trace(_favSaved);
			_optClicker = this["optClicker"];
			_prevClicker = this["prevClicker"];
			_favClicker = this["favClicker"];
			_nextClicker = this["nextClicker"];
			_delayClicker = this["delayClicker"];
			_tmClicker = this["tmClicker"];
			_inprogressClip = this["inprogressclip"];
			_remainMarker = this["remainMarker"];
			_arrowLeft = this["arrowLeft"];
			_arrowRight = this["arrowRight"];
			_arrowRight.alpha = _arrowLeft.alpha = 0;
			_inprogressClip.alpha = 0;
			_inprogressClip.visible = false;
			_favSaved.alpha = 0;
			_optClicker.addEventListener(MouseEvent.CLICK, stepUpOption);
			
			_delayClicker.addEventListener(MouseEvent.CLICK, handleDelayControls);
			_prevClicker.addEventListener(MouseEvent.CLICK, stepDownActiveOption);
			_nextClicker.addEventListener(MouseEvent.CLICK, stepUpActiveOption);
			_favClicker.addEventListener(MouseEvent.CLICK, cloneActiveProgram);
			_tmClicker.addEventListener(MouseEvent.CLICK, handleTimeManager);
			controller.addEventListener(ELEvent.PROGRAM_STARTED, onProgramStartPause);
			controller.addEventListener(ELEvent.PROGRAM_PAUSED, onProgramStartPause);
			
		}
		protected function handleTimeManager(evt:MouseEvent):void{
			_tmOption = controller.timeManager;
			
			if(_tmOption.selections.length == 0) return;
			activeTimeManagerOption();
		}
		protected function handleDelayControls(evt:MouseEvent):void{
			activateDelayOption();
		}
		protected function onProgramStartPause(evt:ELEvent):void{
			TweenMax.to(_inprogressClip, .4, {alpha:(evt.type == ELEvent.PROGRAM_STARTED) ? .8 : 0, ease:Strong.easeInOut});
		}
		override protected function renderOptionItems():void{
			
			_currentOpt = -1;
			_tmActive = _delayActive = false;
			assignReferences();
			_optionIndicator.clearItems();
			_optionclips = [getDefinitionByName("TempClip"), getDefinitionByName("SpinClip"), getDefinitionByName("SoakClip"), getDefinitionByName("RinseClip")]
			_optionReferences = new Array();
			var sx:Number = 77;
			var counter:Number = 0;
			for each (var opts:ELOption in _optionSet){
				if(opts.included){
					var ref:ELDigitalOptionItem = new _optionclips[counter]();
					ref.addEventListener(ELUIEvent.OPTION_CHANGED, onOptionChanged);
					ref.option = opts;
					ref.x = sx;
					ref.y = 13;
					sx += 50;
					counter++;
					_optionReferences.push(addChild(ref));
				}	
			}
			
			
			//activateOption();
			
		}
		private function hideFavSaved():void{
			TweenMax.to(_favSaved, 1, {alpha:0, ease:Strong.easeInOut, delay:4});
		}
		private function cloneActiveProgram(evt:MouseEvent):void{
			controller.replaceProgramWithAlias("Favourites", controller.cloneActiveProgram("Favourites"));
			TweenMax.killTweensOf(_favSaved);
			TweenMax.to(_favSaved, 1, {alpha:1, ease:Strong.easeInOut, onComplete:hideFavSaved});
		}
		private function getCurrentOption():ELOption{
			if(_delayActive){
				return _delayOption;
			}
			if(_tmActive){
				return _tmOption;	
			}
			return _activeOption.option;
		}
		private function resolveCurrentOption(current:Number):void{
			var _optionRef:ELOption = getCurrentOption();
			var sels:Array = !_tmActive ? _optionRef.selections : getTimeManagerSelection(); 
			if(!_delayActive && !_tmActive){
				_activeOption.option.customSelected = null;
				_activeOption.option.selected = sels[current].key;
				_activeOption.updateTextValue();
			}else{
				
				_optionRef.selected = sels[current].key;
				if(_tmActive){
					_optionRef.customSelected = sels[current];
				}else{
					_optionRef.customSelected = null;
				}
			}
			_optionIndicator.updateSelection();
			_activeOption.dispatchEvent(new ELUIEvent(ELUIEvent.OPTION_CHANGED, _optionRef));
		}
		private function cloneOption(o:Object):Object{
			var newo:Object = {};
			for (var prop:String in o){
				newo[prop] = o[prop];
			}
			return newo;
		}
		private function getTimeManagerSelection():Array{
			var retarr:Array = new Array;
			var sels:Array = _tmOption.selections;
			var count:Number = 0;
			for each(var o:Object in sels){
				if(Number(o.value) < controller.activeProgramTime || Number(o.value) == 0){
					retarr[count] = cloneOption(o);
					retarr[count].index = count;
					count++;
				}
			}
			return retarr;
		}
		private function checkTimeManagerCurrent(current:Number, up:Boolean, sels:Array):Number{
			
			if(up){
				//if(current < _tmOption.selections.length-1){
				if(current < sels.length-1){
					current++;
				}else{
					current = 0;
				}
				
			}else{
				if(current > 0){
					current--;
				}else{
					current = sels.length-1;
				}
				
			}
			//its fine..default values
			if(isNaN(Number(sels[current].value))) return current;
			//check if value is greater than the current time 
			if(Number(sels[current].value) > controller.activeProgramTime){
				current = checkTimeManagerCurrent(current, up, getTimeManagerSelection());
			}
			
			return current;
		}
		private function handleTimeManagerSteps(up:Boolean = true):void{
			var _optionRef:ELOption = getCurrentOption();
			var current:Number = _optionRef.selected.index;
			current = checkTimeManagerCurrent(current, up, getTimeManagerSelection());
			
			resolveCurrentOption(current);
			checkArrows(current); 
			
		}
		private function stepUpActiveOption(evt:MouseEvent):void{
			if(_currentOpt == -1) return;
			if(_tmActive){
				handleTimeManagerSteps();
				return;
			}
			var _optionRef:ELOption = getCurrentOption();
			var sels:Array = _optionRef.selections;
			var current:Number = _optionRef.selected.index;
			var max:Number = sels.length-1;
			if(current < max){
				current++;
			}else{
				current = 0;
			}
			resolveCurrentOption(current);
			checkArrows(current, max);
			
		} 
		private function stepDownActiveOption(evt:MouseEvent):void{
			if(_currentOpt == -1) return;
			if(_tmActive){
				handleTimeManagerSteps(false);
				return;
			}
			var _optionRef:ELOption = getCurrentOption();
			var sels:Array = _optionRef.selections;
			var current:Number = _optionRef.selected.index;
			var max:Number = sels.length-1;
			if(current > 0){
				current--;
				
			}else{
				current = max;
			}
			resolveCurrentOption(current);
			checkArrows(current, max);
			
		}
		private function checkArrows(current:Number, umax:Number = NaN):void{
			
			if(_arrowLeft.alpha == 0){
				TweenMax.to(_arrowLeft, .4, {alpha:1, ease:Strong.easeInOut});
				TweenMax.to(_arrowRight, .4, {alpha:1, ease:Strong.easeInOut});
			}
			return;
			var _optionRef:ELOption = getCurrentOption();
			var ar:Number = 1;
			var al:Number = 1;
			if(current == (umax || _optionRef.selections.length-1)){
				ar = 0;	
			}else if(current == 0){
				al = 0;
			}
			TweenMax.killTweensOf(_arrowLeft);
			TweenMax.killTweensOf(_arrowRight);
			TweenMax.to(_arrowLeft, .4, {alpha:al, ease:Strong.easeInOut});
			TweenMax.to(_arrowRight, .4, {alpha:ar, ease:Strong.easeInOut});
		}
		private function checkValidOption():void{
			var opt:ELOption = _optionReferences[_currentOpt].option;
			if(opt.selections.length == 0){
				if(_maxOpt > 1){
					if(_currentOpt+1 <= _maxOpt){
						_currentOpt++;
					}else{
						_currentOpt = 0;
					}
					checkValidOption();
				}
				return;
			}
			return;
		}
		private function stepUpOption(evt:MouseEvent):void{
			if(!_delayActive){
				if(_currentOpt < _maxOpt){
					_currentOpt++;
				}else{
					_currentOpt = 0;
				}
			}
			_delayActive = false;
			activateOption();
		}
		private function createDelayOption():void{
			var xm:XML = new XML("<options/>");
			xm.@name = "Delay";
			var opts:XMLList = new XMLList();
			for (var i:Number = 0; i<5;i++){
				opts[i] = new XML("<option/>");
				opts[i].@key = String(i);
				opts[i].@value = String(i);
				opts[i].@enabled = "1";
				opts[i].@time = String(i*60);
				if(i == 0){
					opts[i].@default = "1";
				}
			}
			xm.option = opts;
			_delayOption = new ELOption(xm);
			
		}
		private function activeTimeManagerOption():void{
			if(_currentOpt == -1) _currentOpt = 0;
			if(_tmActive){
				_tmActive = false;
				activateOption();
				return;
			}
			_delayActive = false;
			_tmActive = true;
			if(_activeOption){
				_activeOption.deactivate();
			}
			//getPossibleTimeManagerValue();
			var checkTM:Array  = getTimeManagerSelection();
			if(_tmOption.selected.index > checkTM.length-1){
				_tmOption.customSelected  = checkTM[checkTM.length-1];
			}
			_optionIndicator.activateOption(_tmOption, 1);
			if(_activeOption == null){
				_activeOption = _optionReferences[_currentOpt];
			}
			_activeOption.dispatchEvent(new ELUIEvent(ELUIEvent.OPTION_CHANGED, _tmOption));
			//resolveCurrentOption(_tmOption.selected.index);
			checkArrows(_tmOption.selected.index);
			
			
		}
		private function activateDelayOption():void{
			
			if(_currentOpt == -1) _currentOpt = 0;
			if(_delayActive)  {
				_delayActive = false;
				activateOption();
				return;
			}
			_delayActive = true;
			_tmActive = false;
		
			if(_activeOption){
				_activeOption.deactivate();
			}
			_optionIndicator.activateOption(_delayOption, 1);
			if(_activeOption == null){
				_activeOption = _optionReferences[_currentOpt];
			}
			_activeOption.dispatchEvent(new ELUIEvent(ELUIEvent.OPTION_CHANGED, _delayOption));
			checkArrows(_delayOption.selected.index);
		}
		private function activateOption():void{
			
			if(_currentOpt == -1) return;
			_delayActive = _tmActive = false;
			checkValidOption();
			
			if(_activeOption && _optionReferences[_currentOpt] != _activeOption){
				_activeOption.deactivate();
			}
			_activeOption = _optionReferences[_currentOpt];
			_activeOption.activate();
			_optionIndicator.activateOption(_activeOption.option, 1);
			_activeOption.dispatchEvent(new ELUIEvent(ELUIEvent.OPTION_CHANGED, _activeOption.option));
			checkArrows(_activeOption.option.selected.index);
		}
		
	}
}